﻿using PacoToolKit.Format;

namespace Protocol.SouthGridProtocol.Decoder
{
    public class ParamsDecoder
    {

        #region 通信参数
        public string E0000100(string data)
        {
            //8+1字节的data
            string ret=string.Empty;
            string mm = data.Substring(16, 2);//获取通道类型

            if (mm.Equals("02") || mm.Equals("04"))
            {
                string port = Formater.HEXtoDEC(Formater.ReverseHexString(data.Substring(0, 4)));//端口号
                string hexIp = Formater.ReverseHexString(data.Substring(4, 8));
                string ip = Formater.HEXtoDEC(hexIp.Substring(0, 2)) + "." + Formater.HEXtoDEC(hexIp.Substring(2, 2)) + "." + Formater.HEXtoDEC(hexIp.Substring(4, 2)) + "." + Formater.HEXtoDEC(hexIp.Substring(6, 2));
                ret = ip + ":" + port;
            }
            else
            {
                ret = string.Empty;
            }

            return ret;
        }
        public string E0000103(string data)
        {
            string port = Formater.HEXtoDEC(Formater.ReverseHexString(data.Substring(0, 4)));//端口号
            string hexIp = Formater.ReverseHexString(data.Substring(4, 8));
            string ip = Formater.HEXtoDEC(hexIp.Substring(0, 2)) + "." + Formater.HEXtoDEC(hexIp.Substring(2, 2)) + "." + Formater.HEXtoDEC(hexIp.Substring(4, 2)) + "." + Formater.HEXtoDEC(hexIp.Substring(6, 2));
            return ip + ":" + port;
        }
        public string E0000104(string data)
        {
            //16字节data，ascii码
            return Formater.ASCLLHexBytesToString(Formater.HexStringToBytesKeep(data));
        }
        public string E0000107(string data)
        {
            //1 byte
            return data;
        }
        public string E0000120(string data)
        {
            //3字节data
            return Formater.ReverseHexString(data);//bcd
        }
        public string E0000121(string data)
        {
            //3字节data
            return Formater.ReverseHexString(data);//bin,但是这里不转换
        }
        public string E0000123(string data) {
            //2字节data
            return Formater.HEXtoDEC(Formater.ReverseHexString(data));//bin，输出十进制端口号
        }
        public string E0000130(string data) {
            //6 byte data
            return data.Substring(10, 2) + "-" + data.Substring(8, 2) + "-" + data.Substring(6, 2) + " " + data.Substring(4, 2) + ":" + data.Substring(2, 2) + ":" + data.Substring(0, 2);
        }

        public string E0000B00(string data)
        {
            return Formater.ReverseHexString(data);
        }
        public string E0000B01(string data)
        {
            string ret = string.Empty;
            ret += Formater.ReverseHexString(data.Substring(0, 4));//bin
            ret += "-";
            ret += int.Parse(data.Substring(4, 2));
            ret += "-";
            ret += Formater.ReverseHexString(data.Substring(6, 6));//bcd 
            return ret;
        }
        public string E0000B02(string data)
        {
            return Formater.ReverseHexString(data);
        }
        #endregion

        #region 测量点参数
        //测量点控制参数
        public string E0800000(string data)
        {
            return int.Parse(data).ToString();
        }
        public string E0800001(string data)
        {
            return int.Parse(data).ToString();
        }
        public string E0800002(string data)
        {
            return Formater.ReverseHexString(data);
        }
        public string E0800003(string data)
        {
            return int.Parse(data).ToString();
        }
        public string E0800004(string data)
        {
            return int.Parse(data).ToString();
        }
        public string E0800005(string data)
        {
            return int.Parse(data).ToString();
        }
        public string E0800006(string data)
        {
            return int.Parse(data).ToString();
        }
        public string E0800007(string data)
        {
            return int.Parse(data).ToString();
        }
        public string E0800008(string data)
        {
            return int.Parse(data).ToString();
        }
        public string E0800009(string data)
        {
            //12 bytes
            return Formater.ReverseHexString(data);
        }
        public string E080000A(string data)
        {
            //1 bytes
            return (int.Parse(Formater.HEXtoDEC(data))).ToString();
        }

        public string E080000B_BB(string data)
        {
            string hexBB = data.Substring(0, 2);
            return (int.Parse(hexBB) * 300).ToString();
        }

        public string E080000B_PP(string data)
        {
            string hexPP = data.Substring(2, 2);
            return int.Parse(hexPP).ToString();
        }

        public string E080000B_DD(string data)
        {
            string hexDD = data.Substring(4, 2);
            return int.Parse(hexDD).ToString();
        }

        public string E080000B_SS(string data)
        {
            string hexSS = data.Substring(6, 2);
            return int.Parse(hexSS).ToString();
        }
        
        public string E080000C(string data)
        {
            //2 bytes
            return int.Parse(Formater.ReverseHexString(data)).ToString(); 
        }
        public string E080000D(string data)
        {
            //2 bytes
            return int.Parse(Formater.ReverseHexString(data)).ToString(); 
        }
        #endregion
    }
}
